/*
 * Created on 12/03/2008
 *
 */
package proxy.load;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.log4j.Logger;

import com.grupobbva.iici.serializable.Corporativa;
import com.grupobbva.iici.serializable.Log;
import com.grupobbva.iici.serializable.Menu;
import com.grupobbva.iici.serializable.Oficina;
import com.grupobbva.iici.serializable.Perfil;
import com.grupobbva.iici.serializable.PermisoPerfil;
import com.grupobbva.iici.session.SessionService;
import com.grupobbva.iici.session.SessionServiceHome;
import com.grupobbva.iici.util.ConstantesUtil;
import com.ibm.etools.service.locator.ServiceLocatorManager;

/**
 * @author P020795
 * 
 */
public class Connection {
	private final static String STATIC_SessionServiceHome_REF_NAME = "ejb/SessionService";
	private final static Class STATIC_SessionServiceHome_CLASS = SessionServiceHome.class;
	InitialContext contextoJNDI;
	
	private static Logger logger = Logger.getLogger(Connection.class.getName());
	
	/*****************************************************************************
	 * REGISTRALOG : LOG
	 /****************************************************************************/

	public boolean registraLog(Log datos) {
		SessionService  aSessionService = createSessionService();
		try {
			aSessionService = createSessionService();
			return aSessionService.registraLog(datos);
		} catch (RemoteException e) {

			logger.error("ERROR", e);
		}
		return false;
	}

	public Vector findAllLog(String fecIni, String fecFin, String codOficina) {
		SessionService  aSessionService = createSessionService();
		try {
			aSessionService = createSessionService();
			return aSessionService.findAllLog(fecIni, fecFin, codOficina);
		} catch (Exception e) {
			logger.error("ERROR", e);
		}

		return null;
	}

	/*****************************************************************************
	 * OFICINA
	 /****************************************************************************/
	public boolean createOficina(Oficina oficina) {
		SessionService  aSessionService = createSessionService();
		try {
			aSessionService = createSessionService();
			return aSessionService.createOficina(oficina);
		} catch (RemoteException e) {

			logger.error("ERROR", e);
		}
		return false;
	}

	public boolean deleteOficina(String codOficina) {
		SessionService  aSessionService = createSessionService();
		try {
		
			return aSessionService.deleteOficina(codOficina);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return false;
	}

	public boolean updateOficina(Oficina oficina) {
		SessionService  aSessionService = createSessionService();
		try {
		return aSessionService.updateOficina(oficina);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return false;
	}

	public Vector findAllOficina() {
		SessionService  aSessionService = createSessionService();
		try {
				return aSessionService.findAllOficina();
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return null;

	}

	public Oficina findIdOficina(String codigoOficina) {
		SessionService  aSessionService = createSessionService();
		try {
			aSessionService = createSessionService();
			return aSessionService.findIdOficina(codigoOficina);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return null;
	}

	/*****************************************************************************
	 * findxCodPerfil : MENU
	 /****************************************************************************/
	public Vector findxCodPerfil(String codPerfil) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findxCodPerfil(codPerfil);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public Vector findAllMenu() {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findAllMenu();
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean deleteMenu(java.math.BigDecimal padre) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.deleteMenu(padre);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	public Menu findxIdMenu(java.math.BigDecimal padre) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findxIdMenu(padre);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean updateMenu(Menu menu) {
		SessionService  aSessionService = createSessionService();
		try {
			
			return aSessionService.updateMenu(menu);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	/******************************************************************************
	 * findPermisoPerfilxTipAccxValorRCP :: PERMISO PERFIL 
	 /******************************************************************************/

	public PermisoPerfil findPermisoPerfilxTipAccxValorRCP(String codReg,
			String codCat, String path) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findPermisoPerfilxTipAccxValorRCP(codReg,
					codCat, path);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public Vector findAllPerfil(int flag) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findAllPerfil(flag);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean createMenu(Menu menu) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.createMenu(menu);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	public Vector findAllPermisoPerfil(int flag) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findAllPermisoPerfil(flag);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean createPermisoPerfil(String codPerfil, BigDecimal tipoAcceso,
			String valorRCP) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.createPermisoPerfil(codPerfil, tipoAcceso,
					valorRCP);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;

	}

	public boolean deletePermisoPerfil(String codPerfil, BigDecimal tipoAcceso,
			String valorRCP) {
		SessionService  aSessionService = createSessionService();
		
		try {
			return aSessionService.deletePermisoPerfil(codPerfil, tipoAcceso,
					valorRCP);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;

	}

	public PermisoPerfil findPermisoPerfilById(String codPerfil,
			BigDecimal tipoAcceso, String valorRCP) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findPermisoPerfilById(codPerfil, tipoAcceso,
					valorRCP);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean updatePermisoPerfil(String codPerfil, BigDecimal tipoAcceso,
			String valorRCP, String cp, BigDecimal ta, String vr) {
		SessionService  aSessionService = createSessionService();
		try {
			boolean aboolean = aSessionService.updatePermisoPerfil(codPerfil,
					tipoAcceso, valorRCP, cp, ta, vr);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	/******************************************************************************
	 * findPerfil :: PERFIL 
	 /******************************************************************************/

	public Perfil findPerfil(String cod_perfil) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findPerfil(cod_perfil);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean updatePerfil(Perfil perfil) {
		SessionService  aSessionService = createSessionService();
		try {
			boolean aboolean = aSessionService.updatePerfil(perfil);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	public boolean deletePerfil(String codPerfil) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.deletePerfil(codPerfil);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	public boolean createPerfil(Perfil perfil) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.createPerfil(perfil);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;

	}

	/******************************************************************************
	 * findAllPerfil :: T. COORPORATIVA 
	 */
	public Corporativa findCorporativaBean(String cod_mult, String cod_ele) {
		SessionService  aSessionService = createSessionService();
		try {
			
			return aSessionService.findCorporativaBean(cod_mult, cod_ele);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return null;
	}
	
	public Vector findByCodMulti(String codMult) {
		SessionService  aSessionService = createSessionService();
		try {
			
			return aSessionService.findByCodMulti(codMult);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean updateCorporativa(Corporativa corporativa) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.updateCorporativa(corporativa);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return false;
	}

	public Corporativa findByID(String id) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.findByID(id);
		} catch (RemoteException ex) {
			logger.error("ERROR", ex);
		}
		return null;
	}

	public boolean deleteCorporativa(Corporativa corporativa) {
		SessionService  aSessionService = createSessionService();
		try {
			return aSessionService.deleteCorporativa(corporativa);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}

		return false;
	}

	public boolean createCorporativa(Corporativa corporativa) {
		SessionService  aSessionService = createSessionService();
		
		try {
			
			return aSessionService.createCorporativa(corporativa);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		}
		return false;
	}
	
	/**END****************************************************************************/
	protected SessionService createSessionService() {
		SessionServiceHome aSessionServiceHome = (SessionServiceHome) ServiceLocatorManager
				.getRemoteHome(STATIC_SessionServiceHome_REF_NAME,
						STATIC_SessionServiceHome_CLASS);
		try {
			if(aSessionServiceHome == null) { 
				contextoJNDI = new InitialContext(System.getProperties());
				logger.info("MGLHPM contextoJNDI " + ToStringBuilder.reflectionToString(contextoJNDI, ToStringStyle.MULTI_LINE_STYLE));
				logger.info("MGLHPM contextoJNDI " + contextoJNDI.getNameInNamespace());
				aSessionServiceHome = instanciarInterfazHome(contextoJNDI);		
			}
			if (aSessionServiceHome != null){
				return aSessionServiceHome.create();
			}
		} catch (javax.ejb.CreateException e) {
			logger.error("ERROR", e);
		} catch (RemoteException e) {
			logger.error("ERROR", e);
		} catch (NamingException e) {
			logger.error("ERROR", e);
		}
		return null;
	}
	
	private SessionServiceHome instanciarInterfazHome(InitialContext contexto) {	    
	 	SessionServiceHome home = null;
	    try {
	 		home = (SessionServiceHome)contexto.lookup(ConstantesUtil.URL_CONTEXT);
	    } catch(NamingException e) {
	    	logger.error("ERROR", e);
	    } catch(Exception e) {
	    	logger.error("ERROR", e);
	    }
	    return home;
	}
}
